package com.tjetc.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.tjetc.common.JsonResult;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //访问controller方法之前 先执行preHandle，所以可以作为是否登录的判断
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //获取session对象
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("admin") == null) {
            //设置中文字符编码和返回值的类型
            response.setHeader("Content-Type","application/json;charset=utf-8");
            JsonResult result = new JsonResult(-1, "未登录或者登录过期，请重新登录", null);
            //把result对象转换成json字符串
            String json = JSONObject.toJSONString(result);
            //把json的字符串输出到前端就可以了
            response.getWriter().write(json);
            //不放行，就意味着不能再访问controller对应的方法了
            return false;
        }
        //放行
        return true;
    }
}
